package com.yellow.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yellow.dao.CheckGroupDao;
import com.yellow.enity.PageResult;
import com.yellow.enity.QueryPageBean;
import com.yellow.pojo.CheckGroup;
import com.yellow.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> checkGroups = checkGroupDao.pageQuery(queryString);
        long total = checkGroups.getTotal();
        List<CheckGroup> list = checkGroups.getResult();
        return new PageResult(total, list);
    }

    @Override
    public void add(CheckGroup checkGroup, int[] checkitemIds) {
        //添加检查组,返回自增注解按
        checkGroupDao.addChecKGroup(checkGroup);
        //添加检查组和检查项的关系
        Map<String, Object> map = new HashMap<>();
        map.put("checkGroupId", checkGroup.getId());
        map.put("checkitemIds", checkitemIds);
        checkGroupDao.addCheckGroupAndCheckItemRel(map);

    }

    @Override
    public CheckGroup findCheckGroupByid(int id) {
        return checkGroupDao.findCheckGroupByid(id);
    }

    @Override
    public List<Integer> findChecKItemIdsByCheckGroupId(int id) {
        return checkGroupDao.findChecKItemIdsByCheckGroupId(id);
    }

    @Override
    public void deleteByid(int id) {
        checkGroupDao.deleteByid(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public void update(CheckGroup checkGroup, int[] checkitemIds) {
        //更新检查组
        checkGroupDao.updateCheckGroup(checkGroup);
        //更新检查组与检查项的关系
        //删除以前的关系
        checkGroupDao.deleteCheckGroupAndCheckItemRelByCheckGroupId(checkGroup.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("checkGroupId", checkGroup.getId());
        map.put("checkitemIds", checkitemIds);
        checkGroupDao.addCheckGroupAndCheckItemRel(map);

    }
}
